<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
    String loginPath = basePath + "indexLogin";
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>违章头条管理系统 首页</title>
    <link rel="stylesheet" href="//cdn.bootcss.com/bootstrap/3.3.5/css/bootstrap.min.css">
    <link rel="shortcut icon" href="http://www.autohome.com.cn/favicon.ico"/>
    <script src="http://cdn.bootcss.com/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    <style>.msg {
        color: #ff0000;
        padding-left: 6px;
        font-size: 12px;
    }</style>
</head>
<body>
<div class="container-fluid"
     style="position:absolute; left:50%; top:50%; margin-top:-130px; margin-left:-150px; width:300px;">
    <ul class="list-group">
        <li class="list-group-item active">违章头条管理系统</li>
        <li class="list-group-item">
            <input type="text" id="nickname" name="nickname" class="form-control" placeholder="账号" required
                   autofocus>
            <input type="password" id="password" name="password" class="form-control" placeholder="密码"
                   style="margin:8px 0 8px 0;" required>
        </li>
        <li class="list-group-item">
            <button class="btn btn-default" type="button" id="subBtn"> 登 录</button>
            <span class="msg"></span>
        </li>

    </ul>

</div>
</body>
</html>
<script>


    function SubmitAction() {
        var un = $("#nickname").val();
        var pw = $("#password").val();
        if (un == '') {
            $('.msg').html("用户名不能为空！");
        } else if (pw == '') {
            $('.msg').html("密码不能为空！");
        }
        else {
            AjaxForm();
        }
    }

    function AjaxForm() {
        $("#subBtn").unbind("click");
        $(".msg").html("登陆验证中，请稍候……");
        var postdata = "nickname=" + $("#nickname").val() + "&password=" + $("#password").val()
        $.ajax({
            type: "POST",
            url: "<%=loginPath%>",
            data: encodeURI(postdata),
            cache: false,
            success: function (msg) {
                if (msg == 'success') {
                    window.location = '<%=basePath%>admin/users';
                }
            },
            error: function () {
                $('.msg').html("登录失败，请重试！").show();
                $('#subBtn').css("cursor", "pointer").bind("click", SubmitAction);
            }

        });
    }
    $(document).ready(function () {
        $("#nickname").focus();
        $("#nickname").keydown(function (event) {
            if (event.keyCode == 13) {
                $("#password").focus();
            }
        });

        $("#password").keydown(function (event) {
            if (event.keyCode == 13) {
                SubmitAction();
            }
        })
    })
</script>